/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LogonFrame.java
 *
 * Created on 18.12.2008, 18:38:36
 */

package gui;

import gui.agent.DocumnetListFrame;
import gui.manager.ManagerFrame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.UIManager;

import ui.logger.GlobalLogger;
import ui.logger.LogEvent;
import ui.logger.LogEvent.Types;
import client.Config;
import client.Factory;
import client.Factory.Connection;

/**
 * This code was edited or generated using CloudGarden's Jigloo
 * SWT/Swing GUI Builder, which is free for non-commercial
 * use. If Jigloo is being used commercially (ie, by a corporation,
 * company or business for any purpose whatever) then you
 * should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details.
 * Use of Jigloo implies acceptance of these licensing terms.
 * A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
 * THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
 * LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
/**
 *
 * @author re_agent
 */
public class LogonFrame extends javax.swing.JFrame {

	/**
	 *
	 */
	private static final long serialVersionUID = -269162760136479266L;
	/** Creates new form LogonFrame */
    public LogonFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        lbName = new javax.swing.JLabel();
        tfName = new javax.swing.JTextField();
        lbPassword = new javax.swing.JLabel();
        tfPassword = new javax.swing.JPasswordField();
        groupPanel = new javax.swing.JPanel();
        rbUDP = new javax.swing.JRadioButton();
        rbTCP = new javax.swing.JRadioButton();
        rbRMI = new javax.swing.JRadioButton();
        rbCORBA = new javax.swing.JRadioButton();
        jbOK = new javax.swing.JButton();
        jbCancel = new javax.swing.JButton();

        buttonGroup1.add(rbUDP);
        buttonGroup1.add(rbTCP);
        buttonGroup1.add(rbRMI);
        buttonGroup1.add(rbCORBA);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		lbName.setText("Имя пользователя");
		lbPassword.setText("Пароль");

        groupPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(
				javax.swing.BorderFactory.createTitledBorder(""),
 "Протокол взаимодействия"));

        rbUDP.setSelected(true);
		rbUDP.setText("Дейтограмные сокеты");
		rbTCP.setText("Потоковые сокеты");
		rbRMI.setText("Удаленный вызов методов");
		rbCORBA.setText("Корба");


        javax.swing.GroupLayout groupPanelLayout = new javax.swing.GroupLayout(groupPanel);
        groupPanel.setLayout(groupPanelLayout);
        groupPanelLayout.setHorizontalGroup(
            groupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(groupPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(groupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(rbTCP, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .addComponent(rbUDP, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .addComponent(rbRMI, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE)
                    .addComponent(rbCORBA, javax.swing.GroupLayout.DEFAULT_SIZE, 240, Short.MAX_VALUE))
                .addContainerGap())
        );
        groupPanelLayout.setVerticalGroup(
            groupPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(groupPanelLayout.createSequentialGroup()
                .addComponent(rbUDP)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbTCP)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbRMI)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(rbCORBA))
        );

		jbOK.setText("Войти");
        jbOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbOKActionPerformed(evt);
            }
        });

		jbCancel.setText("Отмена");
		jbCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				System.exit(0);
			}
		});

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
		this.setTitle("\u0412\u0445\u043e\u0434");
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(groupPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbName)
                            .addComponent(lbPassword))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(tfPassword, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
                            .addComponent(tfName, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jbCancel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbOK)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbName)
                    .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbPassword)
                    .addComponent(tfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(groupPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbCancel)
                    .addComponent(jbOK))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents


    private void jbOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbOKActionPerformed
		try {
			if (rbCORBA.isSelected())
				Factory.init(Connection.CORBA);
			else if (rbRMI.isSelected())
				Factory.init(Connection.RMI);
			else if (rbTCP.isSelected())
				Factory.init(Connection.TCP);
			else if (rbUDP.isSelected())
				Factory.init(Connection.UDP);

			String res = Factory.getFactory().getLogon().logon(
					tfName.getText(),
					server.GlobalConfig.shaHash(new String(tfPassword.getPassword())), "");

			if ("agent".equals(res)) {
				Config.setUsername(tfName.getText());
				setVisible(false);
				DocumnetListFrame documnetListFrame = new DocumnetListFrame();
				documnetListFrame.setVisible(true);
			} else if ("manager".equals(res)) {
				Config.setUsername(tfName.getText());
				setVisible(false);
				new ManagerFrame().setVisible(true);
			}else {
				GlobalLogger.getInstance().show(new LogEvent(Types.ERROR, "Неверные имя пользователя или пароль"));
			}
			
		} catch (RemoteException e) {
			GlobalLogger.getInstance().show(new LogEvent(e));
			e.printStackTrace();
		}catch (NullPointerException e){
			
		}catch (RuntimeException e) {
			GlobalLogger.getInstance().show(new LogEvent(e));
			e.printStackTrace();
		}
}//GEN-LAST:event_jbOKActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LogonFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JPanel groupPanel;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbOK;
    private javax.swing.JLabel lbName;
    private javax.swing.JLabel lbPassword;
    private javax.swing.JRadioButton rbCORBA;
    private javax.swing.JRadioButton rbRMI;
    private javax.swing.JRadioButton rbTCP;
    private javax.swing.JRadioButton rbUDP;
    private javax.swing.JTextField tfName;
    private javax.swing.JPasswordField tfPassword;
    // End of variables declaration//GEN-END:variables

}
